package com.cai.primeton.service;

import com.cai.primeton.entity.PUser;
import com.cai.primeton.repository.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/7/1 13 : 45
 * @package：com.cai.primeton.service
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepo userRepo;
    @Override
    public PUser findByUsernameAndPassword(String username,String password) {
        return userRepo.findByUsernameAndPassword(username,password);
    }

    @Override
    public List<PUser> findAll() {
        List<PUser> all = userRepo.findAll();
        all = all.stream().filter(item -> item.getRoleid() != 1).collect(Collectors.toList());
        return all;
    }

    @Override
    public void save(PUser pUser) {
        userRepo.save(pUser);
    }

    @Override
    public void deleteUser(Integer id) {
        userRepo.deleteById(id);
    }

    @Override
    public PUser findById(Integer id) {
        Optional<PUser> byId = userRepo.findById(id);
        return byId.get();
    }
}
